﻿using ASmile.Com.MDI;
using ASmile.DBModel;
using ASmile.Extend;
using System;
using System.Collections.Generic;
using System.Text;

namespace ASmile.Com.Da
{
    public class MDIBLL : BLLBase, IMDIBLL
    {
        /// <summary>
        /// 获取当前角色的所有菜单
        /// </summary>
        public List<Sys_RoleMenu> GetMenuByRole()
        {
            //string roleId = CurrRoleId;
            var query = DB.CreateQuery<Sys_RoleMenu>();
            query.WhereAnd(s => s.RoleId == CurrRoleId);
            query.OrderBy(s => s.OrderNo);
            var rList = DB.Query(query).ToList(true);
            return rList;
        }

        //public List<Sys_Role_Menu> GetMenuByRole()
        //{
        //    return new MenuRoleBLL().GetMenuByRole().data;
        //}

        public ApiResult LoginOut()
        {
            return RetOK("OK");
        }

        public ApiResult VerifyPwd(string pwd)
        {
            if (DB.QueryExist<Sys_User>(s => s.Id == CurrUserId && s.LoginPwd == pwd))
            {
                return RetOK("OK");
            }
            return RetErr("fail");
        }

        public ApiResult ChangePwd(ArgChangePwd arg)
        {
            if (arg.OldPwd.IsEmpty())
            {
                return RetErr("原密码不能为空");
            }
            if (arg.NewPwd1.IsEmpty())
            {
                return RetErr("新密码不能为空");
            }
            if (arg.NewPwd1 != arg.NewPwd2)
            {
                return RetErr("两次密码输入不一致");
            }

            Sys_User user = new Sys_User
            {
                Id = CurrUserId,
                LoginPwd = arg.NewPwd1
            };
            var r = DB.Update(user, s => s.Id == CurrUserId && s.LoginPwd == arg.OldPwd);
            if (r.RowCount == 1)
            {
                return RetOK("修改密码成功");
            }
            return RetErr("原密码不正确");
        }
    }
}
